package com.itheima.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;


public class JDBCDemo_Connection {
    public void testConnection() throws Exception {
        //1.注册驱动
        Class.forName("com.mysql.jdbc.Driver");

        //2.获取连接               // localhost
        String url = "jdbc:mysql://127.0.0.1:3308/db1?useSSL=false";// &characterEncoding=UTF-8
                    //jdbc:mysql:///db1?useSSL=false
        String username = "root";
        String password = "123456";
        Connection conn = DriverManager.getConnection(url,username,password);

        //3.定义sql
        String sql1 = "update account set money = 3000 where id=1";
        String sql2 = "update account set money = 3000 where id=2";

        //4.获取执行sql的对象 Statement();
        Statement stmt =  conn.createStatement();

        try {
            // 开启事务
            conn.setAutoCommit(false);

            //5.执行sql
            int count1 = stmt.executeUpdate(sql1);//受影响的行数

            //6.处理结束
            System.out.println(count1);

            //5.执行sql
            int count2 = stmt.executeUpdate(sql2);//受影响的行数

            //6.处理结束
            System.out.println(count2);

            //提交事务
            conn.commit();
        } catch (Exception throwables) {
            //回滚事务
            conn.rollback();
            throwables.printStackTrace();
        }

        //提交事务

        //7.释放资源
        stmt.close();
        conn.close();

    }

}
